package com.sr.examination.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.View;

import com.sr.examination.R;
import com.sr.examination.common.widget.NavigationBottomView;
import com.sr.examination.home.HomeFragment;
import com.tl.commonlibrary.ui.BaseFragment;
import com.tl.commonlibrary.ui.BaseFragmentActivity;

public class MainActivity extends BaseFragmentActivity implements NavigationBottomView.OnItemClickListener {
    public static void start(Context context) {
        Intent starter = new Intent(context, MainActivity.class);
        context.startActivity(starter);
    }
    public static final String ITEM_INDEX_KEY = "itemIndex";
    public static final int TAG_TRIGGER_MENU = 1;
    private MenuModel menuModel;//左侧菜单
    private MainContentViewModel containerModel;//内容区域
    private NavigationBottomView navigationView;//底部导航栏,当前app已隐藏底部导航栏
    private DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        containerModel = new MainContentViewModel(this);
        menuModel = new MenuModel(this);
        navigationView = (NavigationBottomView) findViewById(R.id.navigationBottomView);
        navigationView.setListener(this);
        if (savedInstanceState != null) {
            containerModel.onRestoreInstanceState(savedInstanceState);
        } else {
            navigationView.setCurrentItem(Item.HOME);
        }
        containerModel.init(savedInstanceState != null);
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    public void closeMenu() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.postDelayed(new Runnable() {
                @Override
                public void run() {
                    drawer.closeDrawer(GravityCompat.START);
                }
            },100);
        }
    }

    public void openMenu() {
        if (!drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.postDelayed(new Runnable() {
                @Override
                public void run() {
                    drawer.openDrawer(GravityCompat.START);
                }
            },100);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Item item = getCurrentItem();
        if (item != null) {
            outState.putInt(ITEM_INDEX_KEY, item.getIndex());
        }
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onItemClick(Item item) {
        containerModel.showFragment(item);
        switch (item) {
            case HOME://首页
                break;
        }
    }

    @Override
    public void onItemClickRepeat(Item item) {
        BaseFragment fragment = containerModel.getFragment(item);
        if (fragment == null) {
            return;
        }
        switch (item) {
            case HOME://首页
                if (fragment instanceof HomeFragment) {
                    ((HomeFragment) fragment).refresh();
                }
                break;
        }
    }

    public Item getCurrentItem() {
        if (navigationView == null) {
            return null;
        }
        return navigationView.getCurrentItem();
    }

    public void setCurrentItem(Item item) {
        if (navigationView != null) {
            navigationView.setCurrentItem(item);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
            containerModel.exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {
        switch (tag) {
            case TAG_TRIGGER_MENU:
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    drawer.openDrawer(GravityCompat.START);
                }
                break;
        }
    }
}
